{extend name="platform/base" /}
{block name="main"}
<section class="panel panel-default">
	<header class="panel-heading font-bold"> 添加品牌 </header>
	<div class="panel-body">
		<form class="form-horizontal" id="goods_brand_form" name="form">
			<div class="form-group">
				<label class="col-sm-2 control-label"><span class="color-red">*</span>品牌名称</label>
				<div class="col-md-2">
					<input type="text" class="form-control" id="brand_name" name="brand_name" value="" required onkeydown="error_hide()">
				</div>
				<p class="error">请输入品牌名称</p>
			</div>
            <div class="line line-dashed line-lg pull-in"></div>
            <div class="form-group">
				<label class="col-sm-2 control-label"><span class="color-red">*</span>品牌首字母</label>
				<div class="col-md-1">
					<input type="text" class="form-control" id="brand_initial" name="brand_initial" value="" required maxlength="1" onkeydown="error_hide()">
				</div>
				<p class="error">品牌首字母必须是单个字母</p>
			</div>
            <div class="line line-dashed line-lg pull-in"></div>
            <div class="form-group">
				<label class="col-sm-2 control-label">是否推荐</label>
				<div class="col-md-4">
					<label class="switch">
                     <input id="brand_recommend" type="checkbox" >
                  <span></span> </label>
				</div>
			</div>
            <div class="line line-dashed line-lg pull-in"></div>
            <div class="form-group">
				<label class="col-sm-2 control-label">品牌图片</label>
				<div class="col-md-2">
					<p class="imglogo-box"><img id="img_logo" src="" class="imglogo img-size"/></p>
	                	<a href="javascript:void(0);" style="display:block;position:relative;z-index:1;">
	                		<span style="width: 97px;height: 34px;position: absolute;left: 0;top: 0;z-index: 2;cursor: pointer;"> 
	                		<input hidefocus="true" size="1" class="input-file" name="file_upload"
								id="logo" nc_type="change_store_label" type="file" style="width: 97px;height: 34px;padding: 0;margin: 0;border: none 0;opacity: 0;filter: alpha(opacity = 0);cursor: pointer;"
								onchange="imgUpload(this);"> 
								<input type="hidden" id="logo_value" value=""/>
							</span>
							<a class="btn btn-default"><i class="fa fa-cloud-upload text"></i><span class="text">上传图片</span></a>
						</a>
				</div>
			</div>
            <div class="line line-dashed line-lg pull-in"></div>
            <div class="form-group">
				<label class="col-sm-2 control-label">品牌推荐广告图</label>
				<div class="col-md-2">
					<p class="imglogo-box"><img id="img_adv" src="" class="imglogo img-size"/></p>
	                	<a href="javascript:void(0);" style="display:block;position:relative;z-index:1;">
	                		<span style="width: 97px;height: 34px;position: absolute;left: 0;top: 0;z-index: 2;cursor: pointer;"> 
	                		<input hidefocus="true" size="1" class="input-file" name="file_upload"
								id="adv" nc_type="change_store_label" type="file" style="width: 97px;height: 34px;padding: 0;margin: 0;border: none 0;opacity: 0;filter: alpha(opacity = 0);cursor: pointer;"
								onchange="imgUpload(this);"> 
								<input type="hidden" id="adv_value" value="" />
							</span>
							<a class="btn btn-default"><i class="fa fa-cloud-upload text"></i><span class="text">上传图片</span></a>
						</a>
				</div>
			</div>
            <div class="line line-dashed line-lg pull-in"></div>
			<div class="form-group">
				<div class="col-sm-4 col-sm-offset-2">
					<!-- <button type="submit" class="btn btn-primary">保存</button> -->
					<a href="javascript:void(0);" onclick="addGoodsBrandAjax()" class="btn btn-primary">保存</a>
				</div>
			</div>
		</form>
	</div>
</section>
<script>

function validation() {
	var brand_name = $("#brand_name");
	var brand_initial = $("#brand_initial");
	var brand_pic = $("#brand_pic");
	var brand_ads = $("#brand_ads");
	if (brand_name.val() == "") {
		brand_name.focus().parent().next().show();

		return false;
	} else {
		brand_name.parent().next().hide();
	}
	
	if (brand_initial.val() == "") {
		brand_initial.focus().parent().next().show();
		return false;
	} else if (brand_initial.val().length != 1) {
		brand_initial.focus().parent().next("span").text("品牌首字母必须是单个字母").show();
		return false;
	} else {
		brand_initial.parent().next().hide();
	}
	return true;
}

	var flag = false;//防止重复提交
	function addGoodsBrandAjax(){
		if (!validation()) {
			return;
		}
		var brand_name = $("#brand_name").val();
		var brand_initial = $("#brand_initial").val();
		if($("#brand_recommend").prop("checked")){
			var brand_recommend = 1;
		}else{
			var brand_recommend = 0;
		}

		var brand_pic = $("#logo_value").val();
		var brand_ads = $("#adv_value").val();
		$.ajax({
			type : "post",
			url : "{:__URL('PLATFORM_MAIN/goods/addgoodsbrand')}",
			data : {
				'brand_name':brand_name,
				'brand_initial':brand_initial,
				'brand_recommend':brand_recommend,
				'brand_pic':brand_pic,
				'brand_ads':brand_ads
			},
			async : true,
			success : function (data) {
				if (data["code"] > 0) {
					showMessage('success', "商品品牌添加成功","{:__URL('PLATFORM_MAIN/goods/goodsbrandlist')}");
				}else{
					showMessage('error', "商品品牌添加失败");
					flag = false;
				}
			}
		});
	}
	/**
	 *图片上传
	 */
	function imgUpload(event) {
		var fileid = $(event).attr("id");
		var data = { 'file_path' : UPLOADCOMMON };
		uploadFile(fileid,data,function(res){
			if(res.code){
				$("#img_"+fileid).attr("src",__IMG(res.data));
				$("#"+fileid+"_value").val(res.data);
				showTip(res.message,"success");
			}else{
				showTip(res.message,"error");
			}
		});
	}
</script>
{/block}